package com.itboye.jinpuyiyuan.activity.me;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.mobileim.channel.event.IWxCallback;
import com.alibaba.mobileim.channel.util.YWLog;
import com.alibaba.mobileim.utility.IMNotificationUtils;
import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.activity.login.FindPasswordActivity;
import com.itboye.jinpuyiyuan.activity.login.LoginActivity;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.IMKit.LoginSampleHelper;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.CameraPopupWindow;
import com.itboye.jinpuyiyuan.widget.CircleImageView;
import com.itboye.jinpuyiyuan.widget.FilterUtil;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.Observable;
import java.util.Observer;

import static com.itboye.jinpuyiyuan.activity.home.finddoctor.DoctorDetailActivity.TAG;

/**
 * 设置
 */
public class SetUpActivity extends BaseActivity implements Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;

    private LoginSampleHelper loginSampleHelper;
    private TextView updateLoginPassword,currentReditionTv,aboutUsTv,outLoginTv;
    private RelativeLayout clearCacheRl;//清除缓存
    private BranchAddressPresenter presenter;
    private PopupWindow cancelCache;
    private String uid;
    private String BCid;
    private TextView numCache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_up);
        initView();
    }

    private void initView() {
        leftTv.setVisibility(View.GONE);
        rightImg.setVisibility(View.GONE);
        tvTitle.setText("设置");
        //init SDK 添加消息接受监听
        loginSampleHelper = LoginSampleHelper.getInstance();
        uid= (String) SPUtils.get(getApplication(),null, Const.UID,"");
        BCid= (String) SPUtils.get(getApplication(),null,Const.ALIBAICHUAN,"");
        presenter=new BranchAddressPresenter(this);

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {

            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;
                //清除缓存
            case R.id.clearCacheRl:
            case R.id.numCache:
               showCancelCachePhonePop();
                break;
               //修改登录密码
            case R.id.updateLoginPassword:
               startActivity(new Intent(SetUpActivity.this, ResetPasswordActivity.class));

                break;

              //当前版本
            case R.id.currentReditionTv:

                break;

               //关于我们
            case R.id.aboutUsTv:

                break;

              //退出登录
            case R.id.outLoginTv:
             presenter.outLogin(uid,"");
                break;

        }
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity resultEntity = handlerError(arg);
        if (resultEntity!=null){

            if (resultEntity.getEventType()== BranchAddressPresenter.By_UserAccount_logout_success){
                FilterUtil.putClearSharePre(SetUpActivity.this);
                loginSampleHelper.loginOut_Sample();
                SPUtils.put(SetUpActivity.this,null,Const.LOGIN_PASSWORD,"");
//                MAlert.alert(resultEntity.getMsg()+resultEntity.getData());
                SetUpActivity.this.finish();
            }else if (resultEntity.getEventType()== BranchAddressPresenter.By_UserAccount_logout_fail){
                MAlert.alert(resultEntity.getMsg());
            }
        }
    }

    public void showCancelCachePhonePop(){
        View inflate = LayoutInflater.from(this).inflate(R.layout.pop_cancelcache, null);
        cancelCache=new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);

        cancelCache.setAnimationStyle(R.style.popupwindow_anim_style);
        cancelCache.setFocusable(true);
        TextView sureTv=inflate.findViewById(R.id.sureTv);
        TextView cancelTv= inflate.findViewById(R.id.cancelTv);
        final TextView cacheNumTv=inflate.findViewById(R.id.cacheNumTv);
        final TextView callNumberTv=inflate.findViewById(R.id.callNumberTv);
        cacheNumTv.setText("缓存大小: "+numCache.getText().toString());
        inflate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelCache.dismiss();
            }
        });
        //取消
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelCache.dismiss();
            }
        });
        //确定
        sureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelCache.dismiss();
                numCache.setText("0.00M");
            }
        });
        inflate.setFocusable(true);//comment by danielinbiti,设置view能够接听事件，标注1
        inflate.setFocusableInTouchMode(true); //comment by
        cancelCache.showAtLocation(inflate, Gravity.TOP,0,0);

    }
}
